/*
 * $Id: NameData.java 8 2007-12-29 18:54:44Z aaron.day $
 */
package org.crazydays.wowarmory.data;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.crazydays.wowarmory.WowArmoryException;

/**
 * NameData
 */
public class NameData
	extends AbstractData
{
	/** pattern */
	private final static Pattern pattern =
		Pattern.compile("var\\s+theCharName\\s+=\\s+\"(.*?)\";");

	/** name */
	protected String name;

	/**
	 * Set name.
	 *
	 * @param Name
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * Get name.
	 * 
	 * @return Name
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * Set.
	 *
	 * @param data Data
	 * @throws WowArmoryException
	 * @see IData#set(byte[])
	 */
	public void set(byte[] data)
		throws WowArmoryException
	{
		if (data == null) {
			throw new IllegalArgumentException("data == null");
		}

		String string = new String(data);

		Matcher matcher = pattern.matcher(string);
		if (matcher.find()) {
			setName(matcher.group(1));
		} else {
			throw new WowArmoryException("Unable to find name");
		}
	}
}
